//package com.xqboss.chain.service;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.toolkit.StringUtils;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.github.pagehelper.PageHelper;
//import com.xqboss.chain.domain.chain.SysUserChain;
//import com.xqboss.chain.domain.chain.UpUpdateChain;
//import com.xqboss.chain.echain.HttpRequest;
//import com.xqboss.chain.mapper.SysUserChainMapper;
//import com.xqboss.chain.mapper.UpUpdateChainMapper;
//import lombok.extern.slf4j.Slf4j;
//import org.redisson.api.RLock;
//import org.redisson.api.RedissonClient;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.List;
//import java.util.concurrent.CountDownLatch;
//import java.util.concurrent.ExecutorService;
//import java.util.concurrent.Executors;
//import java.util.concurrent.TimeUnit;
//
///**
// * <p>
// * 藏品上链表 服务类
// * </p>
// *
// * @author ct
// * @since 2022-12-13
// */
//@Service
//@Slf4j
//public class UpUpdateChainService extends ServiceImpl<UpUpdateChainMapper, UpUpdateChain> {
//
//    final static String UPCHAINLOCK = "up_chain_lock_";
//
//    @Autowired
//    RedissonClient redissonClient;
//
//    @Autowired
//    private SysUserChainService sysUserChainService;
//
//    /**
//     * 自动上链操作逻辑
//     */
//    public void autoUpChain(){
//        PageHelper.startPage(1, 200000);
//        List<UpUpdateChain> list = list();
//        if(list.isEmpty()){
//            return;
//        }
//
//        // 12个线程进行上链操作
//        int runSize = 12;
//        CountDownLatch countDownLatch = new CountDownLatch(runSize);
//        ExecutorService executorService = Executors.newFixedThreadPool(runSize);
//        for (UpUpdateChain upUpdateChain : list) {
//            executorService.execute(() -> {
//                upchain(upUpdateChain);
//            });
//        }
//    }
//
//    public void upchain(UpUpdateChain upUpdateChain) {
//        String lockKey = UPCHAINLOCK + upUpdateChain.getId() + upUpdateChain.getToUserId();
//        RLock lock = redissonClient.getLock(lockKey);
//        boolean tryLock = false;
//        try {
//            tryLock = lock.tryLock(5, 360, TimeUnit.SECONDS);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        if (!tryLock) {
//            return;
//        }
//        try {
//            //TODO 创建日志
//            UpUpdateChain updateChain = getById(upUpdateChain.getId());
//            if(updateChain != null){
//                this.autoUpChain(upUpdateChain);
//            }
//            // TODO  更新日志
//        } catch (Exception e) {
//            // TODO  更新日志
//            e.printStackTrace();
//        } finally {
//            if (tryLock) lock.unlock();
//        }
//    }
//
//
//
//    /**
//     * 上链或者转链
//     */
//    @Transactional
//    public void autoUpChain(UpUpdateChain upUpdateChain) {
//        Long userId = upUpdateChain.getToUserId();
//        Integer type = upUpdateChain.getNftType();
//        //查看用户有没有链地址
//        SysUserChain sysUserChain = sysUserChainService.getById(userId);
//        if (sysUserChain == null) {//可能是新注册的用户
//            sysUserChain = sysUserChainService.addUserChain(userId);
//        }
//        //3.判断是上链还是转移
//
////        List<UpUpdateChain> list = this.list(wrapper);
//
////        JSONObject object = new JSONObject();
//        String tokenId = null;
//        if (type.equals(0)) {
//            tokenId = one.getSysMemberNftId().toString();
//        } else {
//            Long tokenIdStr = one.getToUserId() + type;
//            tokenId = tokenIdStr.toString();
//        }
//        //保存日志
//        UpUpdateChainLog upUpdateChainLog = upUpdateChainLogService.getOne(new LambdaQueryWrapper<UpUpdateChainLog>().eq(UpUpdateChainLog::getUpUpdateChainId, one.getId()));
//        if (upUpdateChainLog == null) {
//            upUpdateChainLog = new UpUpdateChainLog();
//            upUpdateChainLog.setUpUpdateChainId(one.getId());
//            upUpdateChainLog.setTokenId(tokenId);
//            upUpdateChainLog.setChainStatus(0);
//            upUpdateChainLogService.save(upUpdateChainLog);
//            one.setIsLog(1);
//            this.updateById(one);
//        } else {
//            upUpdateChainLog = upUpdateChainLogService.getById(upUpdateChainLog.getId());
//            //生成
//            if (upUpdateChainLog.getChainStatus().equals(1)) {
//                String transactionReceipt = this.getTransactionReceipt(upUpdateChainLog.getReqNo(), upUpdateChainLog);
//                if (StringUtils.isNotBlank(transactionReceipt)) {
//                    upUpdateChainLog.setChainStatus(3);
//                    upUpdateChainLogService.updateById(upUpdateChainLog);
//                    UpUpdateChain byId = this.getById(one.getId());
//                    byId.setDdcId(tokenId);
//                    byId.setNftAddr(transactionReceipt);
//                    this.updateById(byId);
//                    return;
//                }
//            }
//
//            //todo 铸造
//            if (upUpdateChainLog.getChainStatus().equals(2)) {
//                String transactionReceipt = this.getTransactionReceipt(upUpdateChainLog.getReqNo(), upUpdateChainLog);
//                if (StringUtils.isNotBlank(transactionReceipt)) {
//                    upUpdateChainLog.setChainStatus(3);
//                    upUpdateChainLogService.updateById(upUpdateChainLog);
//                    UpUpdateChain byId = this.getById(one.getId());
//                    byId.setDdcId(tokenId);
//                    byId.setNftAddr(transactionReceipt);
//                    this.updateById(byId);
//                    return;
//                } else {
////                    ERC721: token already minted
//                    //如果是已经铸造过的那么换个tokenid继续
//                    if (upUpdateChainLog.getErrorMessage() != null && upUpdateChainLog.getErrorMessage().indexOf("token already minted") != -1) {
//                        Long s = System.currentTimeMillis();
//                        tokenId = s.toString();
//                    }
//                    //铸造
//                    this.casting(sysUserChain, upUpdateChainLog, tokenId);
//                    return;
//                }
//            }
//
//            if (upUpdateChainLog.getChainStatus().equals(3)) {
//                return;
//            }
//
//
//        }
//        String expandFields = one.getExpandFields();
//        if (expandFields != null) {
//            JSONObject jsonObject1 = JSONObject.parseObject(expandFields, JSONObject.class);
//            if (jsonObject1 != null && jsonObject1.containsKey("isUpChain")) {
//                Integer isUpChain = jsonObject1.getInteger("isUpChain");
//                if (isUpChain.equals(0)) {
//                    //转链
//                    Long fromSysMemberNftId = jsonObject1.getLong("fromSysMemberNftId");
//                    this.chainTransfer(one, fromSysMemberNftId, upUpdateChainLog);
//                    return;
//                } else {
//                    //发新链
//                    //铸造
//                    this.casting(sysUserChain, upUpdateChainLog, tokenId);
//                    return;
//                }
//            }
//        }
//
//        //铸造
//        this.casting(sysUserChain, upUpdateChainLog, tokenId);
//
//    }
//
//}
